package com.spark.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spark.common.core.Result;
import com.spark.common.core.api.SparkCommonApi;
import com.spark.common.core.entity.system.SysDictItem;
import com.spark.common.query.QueryGenerator;
import com.spark.modules.system.service.ISysDictItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 系统数据字典项Controller
 *
 * @author LYCHEE
 * @date 2025/02/20 10:49
 */
@RestController
@RequestMapping(value = "/sys/dictItem")
public class SysDictItemController {

    @Resource
    private ISysDictItemService sysDictItemService;
    @Resource
    private SparkCommonApi sparkCommonApi;

    /**
     * 分页列表查询
     *
     * @param sysDictItem 系统数据字典项对象
     * @param pageNo   分页
     * @param pageSize 分页
     * @return 系统数据字典项列表
     */
    @PreAuthorize("@sp.hasPerm('sys:dictItem:query')")
    @GetMapping(value = "/queryPageList")
    public Result<?> queryPageList(SysDictItem sysDictItem,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<SysDictItem> queryWrapper = QueryGenerator.initQueryWrapper(sysDictItem);
        Page<SysDictItem> page = new Page<SysDictItem>(pageNo, pageSize);
        IPage<SysDictItem> pageList = sysDictItemService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 列表查询
     *
     * @param sysDictItem 系统数据字典项对象
     * @return 系统数据字典项列表
     */
    @PreAuthorize("@sp.hasPerm('sys:dictItem:query')")
    @GetMapping(value = "/queryList")
    public Result<?> queryList(SysDictItem sysDictItem) {
        QueryWrapper<SysDictItem> queryWrapper = QueryGenerator.initQueryWrapper(sysDictItem);
        List<SysDictItem> list = sysDictItemService.list(queryWrapper);
        return Result.ok(list);
    }

    /**
     * 通过ID查询
     *
     * @param id 主键
     * @return 系统数据字典项对象
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam String id) {
        SysDictItem sysDictItem = sysDictItemService.getById(id);
        if (Objects.isNull(sysDictItem)) {
            return Result.fail("数据获取失败");
        }
        return Result.ok(sysDictItem);
    }

    /**
     * 新增
     *
     * @param sysDictItem 系统数据字典项对象
     * @return 成功或失败
     */
    @PreAuthorize("@sp.hasPerm('sys:dictItem:add')")
    @Transactional(rollbackFor = {Exception.class})
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody SysDictItem sysDictItem) {
        boolean save = sysDictItemService.save(sysDictItem);
        if (save){
            sparkCommonApi.refreshDictCache(true);
        }
        return Result.flag(save);
    }

    /**
     * 编辑
     *
     * @param sysDictItem 系统数据字典项对象
     * @return 成功或失败
     */
    @PreAuthorize("@sp.hasPerm('sys:dictItem:edit')")
    @Transactional(rollbackFor = {Exception.class})
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody SysDictItem sysDictItem) {
        boolean update = sysDictItemService.updateById(sysDictItem);
        if (update){
            sparkCommonApi.refreshDictCache(true);
        }
        return Result.flag(update);
    }

    /**
     * 通过id删除
     *
     * @param id 主键
     * @return 成功或失败
     */
    @PreAuthorize("@sp.hasPerm('sys:dictItem:del')")
    @Transactional(rollbackFor = {Exception.class})
    @DeleteMapping(value = "/delete/{id}")
    public Result<?> delete(@PathVariable(name = "id") String id) {
        boolean update = sysDictItemService.removeById(id);
        if (update){
            sparkCommonApi.refreshDictCache(true);
        }
        return Result.flag(update);
    }

    /**
     * 通过id批量删除删除
     *
     * @param ids 主键
     * @return 成功或失败
     */
    @Transactional(rollbackFor = {Exception.class})
    @DeleteMapping(value = "/deleteBatchByIds/{ids}")
    public Result<?> deleteBatchByIds(@PathVariable(name = "ids") String[] ids) {
        boolean update = sysDictItemService.removeBatchByIds(Arrays.asList(ids));
        if (update){
            sparkCommonApi.refreshDictCache(true);
        }
        return Result.flag(update);
    }

}
